﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UWCSE.CompleterVSPackage
{
    /// <summary>
    /// Interaction logic for EnterQueryControl.xaml
    /// </summary>
    public partial class EnterQueryControl : UserControl
    {
        private readonly Action<string> executeQuery;
        private readonly Action cancelQuery;

        public EnterQueryControl(Action<string> executeQuery, Action cancelQuery, string initialQuery = "")
        {
            InitializeComponent();
            queryTextBox.Text = initialQuery;

            this.executeQuery = executeQuery;
            this.cancelQuery = cancelQuery;
        }

        private void executeButton_Click(object sender, RoutedEventArgs e)
        {
            executeQuery(queryTextBox.Text);
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            cancelQuery();
        }

        private void queryTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            // Execute query on [enter], cancel on [esc]
            if (e.KeyboardDevice.Modifiers.Equals(ModifierKeys.None))
            {
                if (e.Key.Equals(Key.Enter))
                {
                    executeQuery(queryTextBox.Text);
                }
                else if (e.Key.Equals(Key.Escape))
                {
                    cancelQuery();
                }
            }
        }
    }
}
